#include "chatcommand.h"
#include "stringbuilder.h"
#include "message.h"
#include "datastring.h"
#include <stdio.h>
#include <inttypes.h>
#include "Debug.h"


message *chatcommand::fromstringbuilder(stringbuilder &output)
{
	// Converts a stringbuilder to a message object.
	message *new_message;
	new_message = new message();
	*new_message = output;	
	return new_message;
}

void chatcommand::incorrect_system_password(chatclient *client,int64_t messageid)
{
	message *new_message;
	stringbuilder output;
		
	output += "error(26,Incorrect system password";
	output += messageid;
	output += ")";
	new_message = new message();
	*new_message = output;
	client->add_message(new_message);
	idisposable::dereference((idisposable**)&new_message);
	client->callback_on_writable();
	return;
}

void chatcommand::success_message(chatclient *client,int64_t messageid)
{
	stringbuilder output;
	message *new_message;
	new_message = new message();
	output += "success(";
	output += messageid;
	output += ")";
	*new_message = output;		
	client->add_message(new_message);
	idisposable::dereference((idisposable**)&new_message);
	client->callback_on_writable();
	return;
}

void chatcommand::not_implemented(chatclient *client)
{
	client->add_message("error(36,That command is not yet implemented.0)");
	client->callback_on_writable();
	return;
}

void chatcommand::parameters_not_correct(chatclient *client,message *received_message)
{
	stringbuilder output;
	message *new_message;
	output += "erorr(";
	output += (45+received_message->actual_message.length);
	output += ",";
	output += "Syntax error in parameters. Original message:";
	output += received_message->actual_message;
	output += "0)";
	new_message = fromstringbuilder(output);
	client->add_message(new_message);
	idisposable::dereference((idisposable**)&new_message);
	client->callback_on_writable();
	return;
}

void chatcommand::message_not_understood(chatclient *client,datastring bad_message)
{
	stringbuilder output;
	message *new_message;    	
	output += "error(";
	output += (bad_message.length + 32);
	output += ",That message was not understood:";
	output += bad_message;
	output += "0)";
	new_message = fromstringbuilder(output);
	client->add_message(new_message);
	idisposable::dereference((idisposable**)&new_message);
	client->callback_on_writable();
	return;
}

void chatcommand::error(chatclient *client,datastring error_message,int64_t messageid)
{
	message *new_message;	
	stringbuilder output;
	new_message = new message();
	output += "error(";
	output.addparameter(error_message);
	output += messageid;
	output +=")";
	
	*new_message = output;
	client->add_message(new_message);
	idisposable::dereference((idisposable**)&new_message);
	client->callback_on_writable();
}
void chatcommand::error(chatclient *client,datastring error_message)
{
	stringbuilder output;
	message *new_message;
	output += "error(";
	output.addparameter(error_message);	
	output +="0)";
	new_message = new message();
	*new_message = output;
	client->add_message(new_message);
	idisposable::dereference((idisposable**)&new_message);
	client->callback_on_writable();
}

message *chatcommand::userjoinedchatroom(
	int64_t messageid,int64_t chatroomid,int64_t chatclientid, user *new_user)
{
	stringbuilder output;
	output += "userjoinedchatroom(";
	output.addparameter(messageid);	
	output.addparameter(chatroomid);
	output.addparameter(chatclientid);
	if (new_user == nullptr) {
		output += "0,0,";
	} else {
		output.addparameter(new_user->userid);
		output.addparameter(*new_user->username);
	}
	output += ")";
	return fromstringbuilder(output);
}

message *chatcommand::userleftchatroom(int64_t messageid,int64_t chatroomid, int64_t chatclientid, user *old_user)
{
	stringbuilder output;
	output += "userleftchatroom(";
	output.addparameter(messageid);
	output.addparameter(chatroomid);
	if (old_user == nullptr) {
		output += "0,0,";
	} else {
		output.addparameter(old_user->userid);
		output.addparameter(*old_user->username);
	}
	output += ")";
	return fromstringbuilder(output);
}

message *chatcommand::chatroomwasdeleted(int64_t messageid,int64_t chatroomid)
{
	Debug debug(__FILE__,__func__,__LINE__);	
	message *new_message;
	stringbuilder output;
		
	output += "chatroomwasdeleted(";
	debug = __LINE__;
	output.addparameter(messageid);
	debug = __LINE__;
	output.addparameter(chatroomid);
	debug = __LINE__;
	output += ")";
	debug = __LINE__;		
	return fromstringbuilder(output);
}
message *chatcommand::chatroomcreated(int64_t messageid,int64_t chatroomid,datastring room_name,int info)
{
	stringbuilder output_message;
	output_message += "chatroomcreated(";		
	output_message.addparameter(messageid);
	output_message.addparameter(chatroomid);
	output_message.addparameter(room_name);
	output_message.addparameter(info);
	output_message += ")";
	
	return fromstringbuilder(output_message);
}

message *chatcommand::yourchatclientid(int64_t chatclientid)
{
	stringbuilder output_message;
	output_message += "yourchatclientid(";		
	output_message.addparameter(chatclientid);
	output_message += ")";
	
	return fromstringbuilder(output_message);
}

message *chatcommand::chat(int64_t messageid,int64_t chatroomid
,int64_t chatclientid,int64_t userid,datastring message_to_send)
{
	stringbuilder output_message;
	
	output_message += "chat(";		
	output_message.addparameter(messageid);
	output_message.addparameter(chatroomid);
	output_message.addparameter(chatclientid);
	output_message.addparameter(userid);
	output_message.addparameter(message_to_send);
	output_message += ")";
	
	return fromstringbuilder(output_message);
}

message *chatcommand::msg(int64_t messageid
	,int64_t chatclientid,int64_t userid,datastring message_to_send)
{
	stringbuilder output_message;
	output_message += "msg(";
	output_message.addparameter(messageid);
	output_message.addparameter(chatclientid);
	output_message.addparameter(userid);
	output_message.addparameter(message_to_send);
	output_message += ")";
	return fromstringbuilder(output_message);
}

message *chatcommand::simplegamestarted(int64_t messageid,datastring gameid)
{
	stringbuilder output_message;
	output_message += "simplegamestarted(";
	output_message.addparameter(messageid);
	output_message.addparameter(gameid);
	output_message += ")";
	return fromstringbuilder(output_message);
}
 
message *chatcommand::simplegamedata(int64_t messageid,datastring gameid,datastring username,datastring gamedata)
{
	stringbuilder output_message;
	output_message += "simplegamedata(";
	output_message.addparameter(messageid);
	output_message.addparameter(gameid);
	output_message.addparameter(username);
	output_message.addparameter(gamedata);
	output_message += ")";
	return fromstringbuilder(output_message);
}
message *chatcommand::simplegameleave(int64_t messageid,datastring gameid,datastring username,datastring gamedata)
{
	stringbuilder output_message;
	output_message += "simplegameleave(";
	output_message.addparameter(messageid);
	output_message.addparameter(gameid);
	output_message.addparameter(username);
	output_message.addparameter(gamedata);
	output_message += ")";
	return fromstringbuilder(output_message);
}
message *chatcommand::simplegamechat(int64_t messageid,datastring gameid,datastring username,datastring chatmessage)
{
	stringbuilder output_message;
	output_message += "simplegamechat(";
	output_message.addparameter(messageid);
	output_message.addparameter(gameid);
	output_message.addparameter(username);
	output_message.addparameter(chatmessage);
	output_message += ")";
	return fromstringbuilder(output_message);
}

message *chatcommand::simplegamejoined(int64_t messageid,datastring gameid,datastring gamedata,biglist<simplechatgameuser *> *userlist)
{
	stringbuilder output_message;
	datastring upordown;
	output_message += "simplegamejoined(";
	output_message.addparameter(messageid);
	output_message.addparameter(gameid);
	output_message.addparameter(gamedata);
	
	biglist_iterator<simplechatgameuser *>userloop(userlist);
	while (!userloop.eof()) {
		output_message.addparameter(*(userloop.item->username));
		upordown = userloop.item->client == nullptr ? "Down" : "Up";
		output_message.addparameter(upordown);
		userloop.movenext();
	}
	output_message += ")";
	return fromstringbuilder(output_message);
}
message *chatcommand::simplegameusers(int64_t messageid,datastring gameid,biglist<simplechatgameuser *> *userlist)
{
	stringbuilder output_message;
	output_message += "simplegameusers(";
	output_message.addparameter(messageid);
	output_message.addparameter(gameid);
	datastring upordown;

	if (userlist != nullptr) {
		biglist_iterator<simplechatgameuser *>userloop(userlist);
		while (!userloop.eof()) {
			output_message.addparameter(*(userloop.item->username));
			upordown = userloop.item->client == nullptr ? "Down" : "Up";
			output_message.addparameter(upordown);
			userloop.movenext();
		}
	}
	output_message += ")";
	return fromstringbuilder(output_message);
}

message *chatcommand::simplegameuserdata(int64_t messageid,datastring gameid,datastring username,datastring userdata)
{
	// A message listing the user data for a user in a game.
	stringbuilder output_message;
	output_message += "simplegameuserdata(";
	output_message.addparameter(messageid);
	output_message.addparameter(gameid);
	output_message.addparameter(username);
	output_message.addparameter(userdata);
	output_message += ")";
	return fromstringbuilder(output_message);
}